package com.tvata.smarttv.ktv.aiopen;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Shahsen on 2020/2/23.
 */
public class AIOpenService extends Service {
    private static final String NOTIFICATION_ID = "1234567890";
    private static final CharSequence NOTIFICATION_NAME = "TEST";
    private String TAG = "AIOpenService";

    AIOpenReceiver aiOpenReceiver = null;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            Bundle data = intent.getExtras();
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action)) {
                switch (action) {
                    case AIOpenConstant.AI_OPEN_ACTION_KTV: {
                        if (data != null) {
                            //play text
                            String command = data.getString("common");
                            if (!TextUtils.isEmpty(command)) {
//                                if ("search".equals(command)) {
//                                    String keyword = data.getString("keyword");
//                                    String singer = data.getString("singerName");
//                                    String songName = data.getString("songName");
//
//                                    Log.d(TAG, "receive :" + command + ", " + keyword + ", singer " + singer + ", song " + songName);
//                                    //todo
//
//                                    Intent intentKtv = new Intent(Intent.ACTION_MAIN);
//                                    ComponentName componentName = new ComponentName("com.tvata.smartv.ktv.test","com.vatata.wae.tvapp.TvAppActivity");
//                                    intentKtv.setComponent(componentName);
//                                    intentKtv.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                                    intentKtv.putExtra("url","./ktv/search.html?command=" +command+ "&singer=" + singer + "&song=" + songName );
//                                    startActivity(intentKtv);
//                                } else if ("control".equals(command)) {
//
//                                }
                                String ctlAction = data.getString("action");
                                String playIndex = data.getString("PlayIndex");
                                String keyword = data.getString("keyword");
                                String singer = data.getString("singerName");
                                String songName = data.getString("songName");

                                Log.d(TAG, "receive :" + command + ", " + ctlAction + ", index " + playIndex + ", " + keyword + ", singer " + singer + ", song " + songName);
                                //todo
                                Intent localIntent = new Intent();
//                                if ("search".equals(command)) {
//                                    localIntent.setAction("com.tvata.play.search");
//                                }else if("control".equals(command)){
                                    localIntent.setAction("com.tvata.play.control");
//                                }
                                JSONObject jsonObject = new JSONObject();
                                try {
                                    jsonObject.put("ctrl_action", ctlAction);
                                    jsonObject.put("play_index", playIndex);
                                    jsonObject.put("command", command);
                                    jsonObject.put("keyword", keyword);
                                    jsonObject.put("singer", singer);
                                    jsonObject.put("songName", songName);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                Bundle broadData = new Bundle();
                                broadData.putString("msg", jsonObject.toString());
                                localIntent.putExtras(broadData);
                                //localIntent.setPackage("com.peasun.aispeechgl");//gl
                                //context.sendBroadcast(localIntent, AIOpenConstant.AI_OPEN_ACTION_PERMISSION);
                                sendBroadcast(localIntent);
                            }

                        }
                    }
                    break;
                }
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        //创建NotificationChannel
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID, NOTIFICATION_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        //Android9.0会报Caused by: java.lang.SecurityException: Permission Denial:
        //android.permission.FOREGROUND_SERVICE---AndroidManifest.xml中需要申请此权限
        startForeground(1, getNotification());

        AIOpenUtils.registerKaraokeApp(this);
        //register receiver
        aiOpenReceiver = AIOpenUtils.registerKaraokeReceiver(this);
    }

    //这个必须加，不能设置为null
    private Notification getNotification() {
        Notification.Builder builder = new Notification.Builder(this)
                .setContentTitle("测试服务")
                .setContentText("我正在运行");//标题和内容可以不加
        //设置Notification的ChannelID,否则不能正常显示
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(NOTIFICATION_ID);
        }
        Notification notification = builder.build();
        return notification;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(aiOpenReceiver);
    }
}
